class WalletTransLog < ApplicationRecord
  belongs_to :timetable, optional: true
  belongs_to :wallet
  has_one :company, through: :wallet, source: :company
  enum action: {paying: 0, recharge: 1, income: 2, rebate: 3, refunded: 4}
  enum kind: {week: 0, detail: 1, late: 2, absent: 3, suspended: 4}
  attr_accessor :report_start_date, :report_end_date, :report_type, :pay_type

  ransacker :action, formatter: proc {|v| actions[v]} do |parent|
    parent.table[:action]
  end
  #批次号，用于查询课表
  def batch_no
    created_at.strftime('%Y%m%d') + ('%06d') % (id % 1000000)
  end

  before_create do
    if pay_type.present?
      self.remark = "[#{pay_type}] - #{remark}"
    end
  end

  after_create do
    wallet.increment!(:amount, amount)
  end
end
